﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using FZKJ.DBUtility;
namespace FZKJ.DAL
{
    /// <summary>
    /// 数据访问类:buss_members
    /// </summary>
    public partial class buss_members
    {
        private readonly SQLHelper sqldal = null;
        public buss_members(string connStr = "")
        {
            sqldal = new SQLHelper(connStr);
        }
        #region  BasicMethod

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string code)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from buss_members");
            strSql.Append(" where code=@code ");
            SqlParameter[] parameters = {
                    new SqlParameter("@code", SqlDbType.VarChar,30)         };
            parameters[0].Value = code;

            return sqldal.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(FZKJ.Model.buss_members model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into buss_members(");
            strSql.Append("id,code,name,sex,cardno,mobile,bloodType,emergencycontact,emergencycontactrelation,emergencycontactmobile,cardpic,servicearea,point,businessattribution,businessdepartment,businessowner,option1,option2,option3,presentaddress,isdel,illnesshistory,grade,receiveaddress,openid,headImg,nickName,city,country,isgloabl,password,lastLoginTime,remark)");
            strSql.Append(" values (");
            strSql.Append("@id,@code,@name,@sex,@cardno,@mobile,@bloodType,@emergencycontact,@emergencycontactrelation,@emergencycontactmobile,@cardpic,@servicearea,@point,@businessattribution,@businessdepartment,@businessowner,@option1,@option2,@option3,@presentaddress,@isdel,@illnesshistory,@grade,@receiveaddress,@openid,@headImg,@nickName,@city,@country,@isgloabl,@password,@lastLoginTime,@remark)");

            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.VarChar,36),
                    new SqlParameter("@code", SqlDbType.VarChar,30),
                    new SqlParameter("@name", SqlDbType.VarChar,100),
                    new SqlParameter("@sex", SqlDbType.SmallInt,2),
                    new SqlParameter("@cardno", SqlDbType.VarChar,36),
                    new SqlParameter("@mobile", SqlDbType.VarChar,11),
                    new SqlParameter("@bloodType", SqlDbType.VarChar,20),
                    new SqlParameter("@emergencycontact", SqlDbType.VarChar,36),
                    new SqlParameter("@emergencycontactrelation", SqlDbType.VarChar,36),
                    new SqlParameter("@emergencycontactmobile", SqlDbType.VarChar,11),
                    new SqlParameter("@cardpic", SqlDbType.VarChar,100),
                    new SqlParameter("@servicearea", SqlDbType.VarChar,100),
                    new SqlParameter("@point", SqlDbType.Int,4),
                    new SqlParameter("@businessattribution", SqlDbType.VarChar,10),
                    new SqlParameter("@businessdepartment", SqlDbType.VarChar,100),
                    new SqlParameter("@businessowner", SqlDbType.VarChar,100),
                    new SqlParameter("@option1", SqlDbType.VarChar,100),
                    new SqlParameter("@option2", SqlDbType.VarChar,100),
                    new SqlParameter("@option3", SqlDbType.VarChar,100),
                    new SqlParameter("@presentaddress", SqlDbType.VarChar,150),
                    new SqlParameter("@isdel", SqlDbType.Int,4),
                    new SqlParameter("@illnesshistory", SqlDbType.Text),
                    new SqlParameter("@grade", SqlDbType.Int,4),
                    new SqlParameter("@receiveaddress", SqlDbType.NVarChar,200),
                    new SqlParameter("@openid", SqlDbType.NVarChar,50),
                    new SqlParameter("@headImg", SqlDbType.NVarChar,200),
                    new SqlParameter("@nickName", SqlDbType.NVarChar,50),
                    new SqlParameter("@city", SqlDbType.NVarChar,50),
                    new SqlParameter("@country", SqlDbType.NVarChar,50),
                    new SqlParameter("@isgloabl", SqlDbType.Int,4),
                    new SqlParameter("@password", SqlDbType.NVarChar,50),
                    new SqlParameter("@lastLoginTime", SqlDbType.DateTime),
                    new SqlParameter("@remark", SqlDbType.NVarChar,200)};
            parameters[0].Value = model.id;
            parameters[1].Value = model.code;
            parameters[2].Value = model.name;
            parameters[3].Value = model.sex;
            parameters[4].Value = model.cardno;
            parameters[5].Value = model.mobile;
            parameters[6].Value = model.bloodType;
            parameters[7].Value = model.emergencycontact;
            parameters[8].Value = model.emergencycontactrelation;
            parameters[9].Value = model.emergencycontactmobile;
            parameters[10].Value = model.cardpic;
            parameters[11].Value = model.servicearea;
            parameters[12].Value = model.point;
            parameters[13].Value = model.businessattribution;
            parameters[14].Value = model.businessdepartment;
            parameters[15].Value = model.businessowner;
            parameters[16].Value = model.option1;
            parameters[17].Value = model.option2;
            parameters[18].Value = model.option3;
            parameters[19].Value = model.presentaddress;
            parameters[20].Value = model.isdel;
            parameters[21].Value = model.illnesshistory;
            parameters[22].Value = model.grade;
            parameters[23].Value = model.receiveaddress;
            parameters[24].Value = model.openid;
            parameters[25].Value = model.headImg;
            parameters[26].Value = model.nickName;
            parameters[27].Value = model.city;
            parameters[28].Value = model.country;
            parameters[29].Value = model.isgloabl;
            parameters[30].Value = model.password;
            parameters[31].Value = model.lastLoginTime;
            parameters[32].Value = model.remark;

            int rows = sqldal.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(FZKJ.Model.buss_members model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update buss_members set ");
            strSql.Append("id=@id,");
            strSql.Append("name=@name,");
            strSql.Append("sex=@sex,");
            strSql.Append("cardno=@cardno,");
            strSql.Append("mobile=@mobile,");
            strSql.Append("bloodType=@bloodType,");
            strSql.Append("emergencycontact=@emergencycontact,");
            strSql.Append("emergencycontactrelation=@emergencycontactrelation,");
            strSql.Append("emergencycontactmobile=@emergencycontactmobile,");
            strSql.Append("cardpic=@cardpic,");
            strSql.Append("servicearea=@servicearea,");
            strSql.Append("point=@point,");
            strSql.Append("businessattribution=@businessattribution,");
            strSql.Append("businessdepartment=@businessdepartment,");
            strSql.Append("businessowner=@businessowner,");
            strSql.Append("option1=@option1,");
            strSql.Append("option2=@option2,");
            strSql.Append("option3=@option3,");
            strSql.Append("presentaddress=@presentaddress,");
            strSql.Append("isdel=@isdel,");
            strSql.Append("illnesshistory=@illnesshistory,");
            strSql.Append("grade=@grade,");
            strSql.Append("receiveaddress=@receiveaddress,");
            strSql.Append("openid=@openid,");

            strSql.Append("headImg=@headImg,");
            strSql.Append("nickName=@nickName,");
            strSql.Append("city=@city,");
            strSql.Append("country=@country,");
            strSql.Append("isgloabl=@isgloabl,");
            strSql.Append("password=@password,");
            strSql.Append("lastLoginTime=@lastLoginTime,");
            strSql.Append("remark=@remark");

            strSql.Append(" where code=@code ");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.VarChar,36),
                    new SqlParameter("@name", SqlDbType.VarChar,100),
                    new SqlParameter("@sex", SqlDbType.SmallInt,2),
                    new SqlParameter("@cardno", SqlDbType.VarChar,36),
                    new SqlParameter("@mobile", SqlDbType.VarChar,11),
                    new SqlParameter("@bloodType", SqlDbType.VarChar,20),
                    new SqlParameter("@emergencycontact", SqlDbType.VarChar,36),
                    new SqlParameter("@emergencycontactrelation", SqlDbType.VarChar,36),
                    new SqlParameter("@emergencycontactmobile", SqlDbType.VarChar,11),
                    new SqlParameter("@cardpic", SqlDbType.VarChar,100),
                    new SqlParameter("@servicearea", SqlDbType.VarChar,100),
                    new SqlParameter("@point", SqlDbType.Int,4),
                    new SqlParameter("@businessattribution", SqlDbType.VarChar,10),
                    new SqlParameter("@businessdepartment", SqlDbType.VarChar,100),
                    new SqlParameter("@businessowner", SqlDbType.VarChar,100),
                    new SqlParameter("@option1", SqlDbType.VarChar,100),
                    new SqlParameter("@option2", SqlDbType.VarChar,100),
                    new SqlParameter("@option3", SqlDbType.VarChar,100),
                    new SqlParameter("@presentaddress", SqlDbType.VarChar,150),
                    new SqlParameter("@isdel", SqlDbType.Int,4),
                    new SqlParameter("@illnesshistory", SqlDbType.Text),
                    new SqlParameter("@grade", SqlDbType.Int,4),
                    new SqlParameter("@receiveaddress", SqlDbType.NVarChar,200),
                    new SqlParameter("@openid", SqlDbType.NVarChar,50),

                    new SqlParameter("@headImg", SqlDbType.NVarChar,200),
                    new SqlParameter("@nickName", SqlDbType.NVarChar,50),
                    new SqlParameter("@city", SqlDbType.NVarChar,50),
                    new SqlParameter("@country", SqlDbType.NVarChar,50),
                    new SqlParameter("@isgloabl", SqlDbType.Int,4),
                    new SqlParameter("@password", SqlDbType.NVarChar,50),
                    new SqlParameter("@lastLoginTime", SqlDbType.DateTime),
                    new SqlParameter("@remark", SqlDbType.NVarChar,200),

                    new SqlParameter("@code", SqlDbType.VarChar,30)};
            parameters[0].Value = model.id;
            parameters[1].Value = model.name;
            parameters[2].Value = model.sex;
            parameters[3].Value = model.cardno;
            parameters[4].Value = model.mobile;
            parameters[5].Value = model.bloodType;
            parameters[6].Value = model.emergencycontact;
            parameters[7].Value = model.emergencycontactrelation;
            parameters[8].Value = model.emergencycontactmobile;
            parameters[9].Value = model.cardpic;
            parameters[10].Value = model.servicearea;
            parameters[11].Value = model.point;
            parameters[12].Value = model.businessattribution;
            parameters[13].Value = model.businessdepartment;
            parameters[14].Value = model.businessowner;
            parameters[15].Value = model.option1;
            parameters[16].Value = model.option2;
            parameters[17].Value = model.option3;
            parameters[18].Value = model.presentaddress;
            parameters[19].Value = model.isdel;
            parameters[20].Value = model.illnesshistory;
            parameters[21].Value = model.grade;
            parameters[22].Value = model.receiveaddress;
            parameters[23].Value = model.openid;

            parameters[24].Value = model.headImg;
            parameters[25].Value = model.nickName;
            parameters[26].Value = model.city;
            parameters[27].Value = model.country;
            parameters[28].Value = model.isgloabl;
            parameters[29].Value = model.password;
            parameters[30].Value = model.lastLoginTime;
            parameters[31].Value = model.remark;

            parameters[32].Value = model.code;

            int rows = sqldal.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string code)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from buss_members ");
            strSql.Append(" where code=@code ");
            SqlParameter[] parameters = {
                    new SqlParameter("@code", SqlDbType.VarChar,30)         };
            parameters[0].Value = code;

            int rows = sqldal.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string codelist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from buss_members ");
            strSql.Append(" where code in (" + codelist + ")  ");
            int rows = sqldal.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public FZKJ.Model.buss_members GetModel(string code)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,code,name,sex,cardno,mobile,bloodType,emergencycontact,emergencycontactrelation,emergencycontactmobile,cardpic,servicearea,point,businessattribution,businessdepartment,businessowner,option1,option2,option3,presentaddress,isdel,illnesshistory,grade,receiveaddress,openid,headImg,nickName,city,country,isgloabl,password,lastLoginTime,remark from buss_members ");
            strSql.Append(" where code=@code ");
            SqlParameter[] parameters = {
                    new SqlParameter("@code", SqlDbType.VarChar,30)         };
            parameters[0].Value = code;

            FZKJ.Model.buss_members model = new FZKJ.Model.buss_members();
            DataSet ds = sqldal.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public FZKJ.Model.buss_members DataRowToModel(DataRow row)
        {
            FZKJ.Model.buss_members model = new FZKJ.Model.buss_members();
            if (row != null)
            {
                if (row["id"] != null)
                {
                    model.id = row["id"].ToString();
                }
                if (row["code"] != null)
                {
                    model.code = row["code"].ToString();
                }
                if (row["name"] != null)
                {
                    model.name = row["name"].ToString();
                }
                if (row["sex"] != null && row["sex"].ToString() != "")
                {
                    model.sex = int.Parse(row["sex"].ToString());
                }
                if (row["cardno"] != null)
                {
                    model.cardno = row["cardno"].ToString();
                }
                if (row["mobile"] != null)
                {
                    model.mobile = row["mobile"].ToString();
                }
                if (row["bloodType"] != null)
                {
                    model.bloodType = row["bloodType"].ToString();
                }
                if (row["emergencycontact"] != null)
                {
                    model.emergencycontact = row["emergencycontact"].ToString();
                }
                if (row["emergencycontactrelation"] != null)
                {
                    model.emergencycontactrelation = row["emergencycontactrelation"].ToString();
                }
                if (row["emergencycontactmobile"] != null)
                {
                    model.emergencycontactmobile = row["emergencycontactmobile"].ToString();
                }
                if (row["cardpic"] != null)
                {
                    model.cardpic = row["cardpic"].ToString();
                }
                if (row["servicearea"] != null)
                {
                    model.servicearea = row["servicearea"].ToString();
                }
                if (row["point"] != null && row["point"].ToString() != "")
                {
                    model.point = int.Parse(row["point"].ToString());
                }
                if (row["businessattribution"] != null)
                {
                    model.businessattribution = row["businessattribution"].ToString();
                }
                if (row["businessdepartment"] != null)
                {
                    model.businessdepartment = row["businessdepartment"].ToString();
                }
                if (row["businessowner"] != null)
                {
                    model.businessowner = row["businessowner"].ToString();
                }
                if (row["option1"] != null)
                {
                    model.option1 = row["option1"].ToString();
                }
                if (row["option2"] != null)
                {
                    model.option2 = row["option2"].ToString();
                }
                if (row["option3"] != null)
                {
                    model.option3 = row["option3"].ToString();
                }
                if (row["presentaddress"] != null)
                {
                    model.presentaddress = row["presentaddress"].ToString();
                }
                if (row["isdel"] != null && row["isdel"].ToString() != "")
                {
                    model.isdel = int.Parse(row["isdel"].ToString());
                }
                if (row["illnesshistory"] != null)
                {
                    model.illnesshistory = row["illnesshistory"].ToString();
                }
                if (row["grade"] != null && row["grade"].ToString() != "")
                {
                    model.grade = int.Parse(row["grade"].ToString());
                }
                if (row["receiveaddress"] != null)
                {
                    model.receiveaddress = row["receiveaddress"].ToString();
                }
                if (row["openid"] != null)
                {
                    model.openid = row["openid"].ToString();
                }
                if (row["country"] != null)
                {
                    model.country = row["country"].ToString();
                }
                if (row["headImg"] != null)
                {
                    model.headImg = row["headImg"].ToString();
                }
                if (row["nickName"] != null)
                {
                    model.nickName = row["nickName"].ToString();
                }

                if (row["city"] != null)
                {
                    model.city = row["city"].ToString();
                }

                if (row["password"] != null)
                {
                    model.password = row["password"].ToString();
                }

                if (row["remark"] != null)
                {
                    model.remark = row["remark"].ToString();
                }

                if (row["isgloabl"] != null && row["isgloabl"].ToString() != "")
                {
                    model.isgloabl = int.Parse(row["isgloabl"].ToString());
                }
                if (row["lastLoginTime"] != null && row["lastLoginTime"].ToString() != "")
                {
                    model.lastLoginTime = DateTime.Parse(row["lastLoginTime"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,code,name,sex,cardno,mobile,bloodType,emergencycontact,emergencycontactrelation,emergencycontactmobile,cardpic,servicearea,point,businessattribution,businessdepartment,businessowner,option1,option2,option3,presentaddress,isdel,illnesshistory,grade,receiveaddress,openid,headImg,nickName,city,country,isgloabl,password,lastLoginTime,remark  ");
            strSql.Append(" FROM buss_members ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return sqldal.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,code,name,sex,cardno,mobile,bloodType,emergencycontact,emergencycontactrelation,emergencycontactmobile,cardpic,servicearea,point,businessattribution,businessdepartment,businessowner,option1,option2,option3,presentaddress,isdel,illnesshistory,grade,receiveaddress,openid,headImg,nickName,city,country,isgloabl,password,lastLoginTime,remark  ");
            strSql.Append(" FROM buss_members ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return sqldal.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM buss_members ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = sqldal.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.code desc");
            }
            strSql.Append(")AS Row, T.*  from (select A.*,B.name as gradename,C.[policynumber],C.[policypicture],[company],[insureanceAgent],rescuecard,[rescuestart],[rescueend],D.servicearea as resArea from buss_members A left join[dbo].[buss_membergrade] B  on A.grade = B.id left join(select * from[dbo].buss_insurance where(islose is null or islose = 0)) C on A.id = C.memberid left join(select * from[dbo].[buss_rescue] where(isdel is null or isdel = 0)) D on A.id = D.memberid) T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            if (endIndex > 0)
                strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return sqldal.Query(strSql.ToString());
        }

        /*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "buss_members";
			parameters[1].Value = "code";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return sqldal.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

        #endregion  BasicMethod
        #region  ExtensionMethod
        public DataTable GetMemberCard(string id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select A.*,B.rescuecard,C.policynumber,C.company,C.insureanceAgent,[numberplate],[VIN],[driving] from [dbo].[buss_members] A left join[dbo].[buss_rescue] B  on A.id = B.memberid left join[dbo].[buss_insurance] C on A.id = C.memberid left join[dbo].[buss_memberCar] D on A.id = D.memberid ");

            strSql.Append(" where B.rescuecard='" + id + "'");
            return sqldal.Query(strSql.ToString()).Tables[0];
        }
        #endregion  ExtensionMethod
    }
}

